package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.entity.DateCondition;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    OrderSettingService orderSettingService;

    @RequestMapping("/findByDateCondition")
    @ExceptionMessage(value = MessageConstant.QUERY_ORDERSETTING_LIST_FAIL)
    public Result findByDateCondition(@RequestBody DateCondition dateCondition){
        //测试异常用
        //int i=2/0;
        List<OrderSetting> orderSettings= null;
            orderSettings = orderSettingService.findByDateCondition(dateCondition);
        return new Result(true, MessageConstant.QUERY_ORDERSETTING_LIST_SUCCESS,orderSettings);
    }

    @RequestMapping("/upload")
    @ExceptionMessage(value = MessageConstant.IMPORT_ORDERSETTING_FAIL)
    public Result addByExcel(@RequestParam("excelFile")MultipartFile multipartFile) throws IOException {
            List<String[]> list = POIUtils.readExcel(multipartFile);
            ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] column : list) {
                String orderDate=column[0];
                String number=column[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.parseInt(number));
                orderSettingList.add(orderSetting);
            }
            orderSettingService.addOrderSettingList(orderSettingList);
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/editNumberByDate")
    @ExceptionMessage(value = MessageConstant.ORDERSETTING_FAIL)
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){

            orderSettingService.editNumberByDate(orderSetting);

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
